<?php
header('content-type:text/html;charset=utf-8');	
	ini_set('date.timezone','Asia/Shanghai');

	require_once $_SITE_PATH_ . "m/m_information.php";
	require_once $_SITE_PATH_ . "m/m_agreement.php";
	require_once $_SITE_PATH_ . "m/m_tasklist.php";
	require_once $_SITE_PATH_."m/m_taskerror.php";
	require_once $_SITE_PATH_.'backend/apis/UpFile_ApiClient.class.php';
			$apiid=isset($_POST['apiid'])?$_POST['apiid']:'';
			$apiflag=isset($_POST['apiflag'])?$_POST['apiflag']:'';
			$error_id=isset($_POST['error_id'])?$_POST['error_id']:'';
			$apitype=0;
			
			file_put_contents($_SITE_PATH_.'backend\apis\SQL.TXT',$apiid.'fff');
			if(!$apiid || !$apiflag)
				{
					exit;
				}
			
			$m_agreement=new m_agreement();
			$m_information=new m_information();
			$m_tasklists=new m_tasklist();
			$m_taskerror=new m_taskerror();
			//$error_id='';
			
			//$m_information->_ssql='select `id`,`code`,process_code,publish,`name`,isbn,bigclass_id,bigclass_name,smallclass_id,smallclass_name,`language`,`version`,price,num,`type`,r_date,editor,saler_sn,factory,author,print_page,publish_time,agreement_id,beizhu,is_upfile,catalog,p_cost,flow,is_volumes,books_nums,face_pic,plat_pic,copr_pic,code_pic,face_thumb,plat_thumb,copr_thumb,code_thumb,goods_file,create_user,create_date,originFile,processFile,finalFile FROM information where `flow`>=9 and ts_flag="1" and `code` is not null and agreement_id is not null and `code`!="" and agreement_id!="" and `code` in ('.$apiid.') order by `id` asc ';
			$m_information->_ssql='select information.id,`code`,process_code,publish,information.name,isbn,bigclass_id,bigclass_name,smallclass_id,smallclass_name,`language`,`version`,price,num,`type`,r_date,editor,saler_sn,factory,author,print_page,publish_time,agreement_id,information.beizhu,is_upfile,catalog,p_cost,flow,is_volumes,books_nums,face_pic,plat_pic,copr_pic,code_pic,shop_price,face_thumb,plat_thumb,copr_thumb,code_thumb,goods_file,information.create_user,information.create_date,originFile,processFile,finalFile,is_vip,words,pages,agreement.start_time,agreement.end_time,authority as authority_id FROM information left join agreement on information.agreement_id=agreement.id where  `flow`>=9 and ts_flag="1" and `code` is not null and agreement_id is not null and `code`!="" and process_code!="" and process_code is not null and agreement_id!="" and words!="" and agreement.start_time!="" and agreement.end_time!="" and `code` in('.$apiid.') ';
			$upfile_array=$m_information->getArray($m_information->query());
			
			//echo $m_information->_ssql;
			//echo $apiid;
			//echo '<br/>';
			//var_dump($upfile_array);
			//exit;
			
			$count=0;
			$count=$m_information->getCount(' `flow`>=9 and ts_flag="1" and `code` is not null and agreement_id is not null and `code`!="" and agreement_id!="" and `code` in ('.$apiid.') ');		
			if(!$count)
				{
					//fopen();'没有数据要更新的！'
					exit;
				}
			//var_dump($upfile_array);
			$update_id='';
			$id='';
			//$count=count($upfile_array);
			//var_dump($upfile_array);
			//echo '<br/>';
			for($i=0;$i<$count;$i++)
				{
					$id.=','.$upfile_array[$i]->id;
					$update_id[$i]['id']=$upfile_array[$i]->id;
					unset($upfile_array[$i]->id);
					
					$m_agreement->_ssql='select authority,`id` from agreement where `id`='.$upfile_array[$i]->agreement_id;
					$agreement=$m_agreement->getArray($m_agreement->query());
					$upfile_array[$i]->authority_id=$agreement[0]->authority;
				}
			$ts_id='';
			
			//echo ' ts_type="0" and product_id='.$id.' and excuflag="0" and ts_flag="0" ';
			//exit;
			if(!empty($id))
				{
					//$id=trim($id,',');
					$cur_count=$m_tasklists->getCount(' ts_type="0" and product_id="'.$id.'" and excuflag!="1" and ts_flag!="1" ');
					if($cur_count<=0 && ($apiflag == 2 || $apiflag == 3))
						{
							$m_tasklists->_ssql='insert into tasklist(ts_type,product_id,excuflag,ts_flag) values("0","'.$id.'","0","0")';
							$m_tasklists->query();
							$m_tasklists->_ssql='select last_insert_id() as lastid';
							$lastid=$m_tasklists->getArray($m_tasklists->query());
							$ts_id=$lastid[0]->lastid;
						}
						elseif($apiflag==1)
							{
								$ts_id=$error_id;
							}
					
					
				} 
			$auth='tjiePjfjvnyGUuyMFEXVPmsewokCauuZ';
			$json_arr=array('data'=>$upfile_array,'count'=>$count);
			$json_arr=json_encode($json_arr);
			
			//var_dump($json_arr);
			//exit;
			
			$time=time();
			$data=array('flag'=>'coprsys','method'=>'copr.add','type'=>'json','timestamp'=>$time,'charset'=>'utf-8','ver'=>1,'copr_data'=>$json_arr);
			$url='http://120.86.185.197/index.php/openapi/rpc/service';
			$api=new UpFile_ApiClient();
			$result=$api->DoRequest($url,$auth,$data);
			
			file_put_contents($_SITE_PATH_.'backend\apis\SQL.TXT',$result.'ssss');
			
			if(get_magic_quotes_gpc())
				{
					$result=stripslashes($result);
				}
			$result=json_decode(trim(urldecode($result),chr(239).chr(187).chr(191)),true);
			var_dump($result);
			exit;
			if(strtolower($result['response'][1])=='success')
				{
					if($apiflag!=1)
						{
							for($i=0;$i<$count;$i++)
								{
									$m_information->_ssql='update information set ts_flag="1" where `id`='.$update_id[$i]['id'];
									$m_information->query();
								}
						}
					
					$m_tasklists->_ssql='update tasklist set excuflag="1",ts_flag="1" where ts_type="'.$apitype.'" and  `id`='.$ts_id;
					$m_tasklists->query();
					//echo $result['info'];
					file_put_contents($_SITE_PATH_.'backend/apis/sql.txt',$m_tasklist->_ssql);
					exit;
				}
				elseif(strtolower($result['response'][0])=='failed')
					{
						$m_tasklists->_ssql='update tasklist set excuflag="0",ts_flag="0" where  ts_type="'.$apitype.'" and  `id`='.$ts_id;
						$m_tasklists->query();
						echo $result['response'][0];
						exit;
					}
					else
						{
							//echo 'FAILED';
							exit;
						}	

		
	

?>